package com.pandora.tv;

import android.app.Application;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.os.Handler;
import android.util.Log;

import com.pandora.tv.Database.LauncherSetting;

public class LauncherApplication extends Application {
	protected LauncherModel mModel = null;
		
	
    @Override
    public void onCreate() {
    	super.onCreate();

        // Register intent receivers*/
        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        filter.addDataScheme("package");
        registerReceiver(mModel, filter);
        filter = new IntentFilter();
        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
        registerReceiver(mModel, filter);

/*        // Register for changes to the favorites
        ContentResolver resolver = getContentResolver(); 
        resolver.registerContentObserver(LauncherSetting.AllProgramsColums.CONTENT_URI, true,
                mFavoritesObserver);
        */

        mModel = new LauncherModel(this);

    }

    /**
     * There's no guarantee that this function is ever called.
     */
    @Override
    public void onTerminate() {
        super.onTerminate();
        mModel = null;

        unregisterReceiver(mModel);

        ContentResolver resolver = getContentResolver();
        resolver.unregisterContentObserver(mFavoritesObserver);
    }

    /**
     * Receives notifications whenever the user favorites have changed.
     */
    private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
        	//((LauncherModel)mModel).startLoader(LauncherApplication.this, false);
        }
    };

    public LauncherModel getModel() {
        return mModel;
    }

/*    LauncherModel setLauncher(LauncherActivity launcher) {
    	((LauncherModel)mModel).initialize(launcher);
        return (LauncherModel)mModel;
    }*/

}
